package com.h.heal.controller;

import com.h.heal.common.MyPageInfo;
import com.h.heal.entity.Comment;
import com.h.heal.entity.Topic;
import com.h.heal.service.CommentService;
import com.h.heal.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/main")
    public ModelAndView main(HttpServletRequest request, ModelAndView mv) {
        mv.setViewName("/admin/comment/main");
        MyPageInfo myPageInfo = commentService.findBySearch(request);
        mv.addObject("pageInfo", myPageInfo);
        return mv;
    }

    @RequestMapping("/user/main")
    public ModelAndView userMain(HttpServletRequest request, ModelAndView mv) {
        mv.setViewName("/user/comment/main");
        MyPageInfo myPageInfo = commentService.findBySearchUser(request);
        mv.addObject("pageInfo", myPageInfo);
        return mv;
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        commentService.deleteById(id);
        return "redirect:/comment/main";
    }

    @RequestMapping("/user/delete/{id}")
    public String userDelete(@PathVariable("id") int id) {
        commentService.deleteById(id);
        return "redirect:/comment/user/main";
    }

    @RequestMapping("/create")
    @ResponseBody
    public String create(Comment comment) {

        return commentService.create(comment);
    }



}
